package com.bicon.botu.mall.goods.commons.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * <p>
 * <p> 线程池配置
 * </p>
 *
 * @author tao.zeng.
 * @since 2018/3/15.
 */
@EnableAsync
@Configuration
public class ThreadPoolTaskExecutorConfig {

    /**
     * 线程池维护线程的最小数量
     */
    private final static int CORE_POOL_SIZE = 10;

    /**
     * 最大线程数
     */
    private final static int MAX_POOL_SIZE = 200;

    /**
     * 队列容量
     */
    private final static int QUEUE_CAPACITY = 100;

    /**
     * 空闲线程的存活时间.
     */
    private final static int KEEP_ALIVE_TIME = 1000;


    @Bean
    public Executor priceDownNoticeAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix("priceDownNotice-");
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor analyzeHealthDietAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix("analyzeHealthDietExecutor-");
        executor.initialize();
        return executor;
    }


    /**
     * 产品信息同步
     *
     * @return
     */
    @Bean
    public Executor sendProductDataAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix("sendProductDataExecutor-");
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor saveSearchWordAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix("saveSearchWordExecutor-");
        executor.initialize();
        return executor;
    }

}
